package cn.zhihong.org.service.impl;

import cn.zhihong.basic.util.PageList;
import cn.zhihong.org.domain.Department;
import cn.zhihong.org.mapper.DepartmentMapper;
import cn.zhihong.org.query.DepartmentQuery;
import cn.zhihong.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional //默认就是强制事务
public class DepartmentServiceImpl implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional(readOnly = true)
    public List<Department> loadAll() {
        return departmentMapper.loadAll();
    }

    @Override
    @Transactional(readOnly = true)
    public Department loadById(Long id) {
        return departmentMapper.loadById(id);
    }

    @Override
    public void save(Department department) {
        departmentMapper.save(department);

    }

    @Override
    public void update(Department department) {
        departmentMapper.update(department);
    }

    @Override
    public void remove(Long id) {
        departmentMapper.remove(id);
    }

    @Override
    public PageList<Department> queryData(DepartmentQuery query) {
        //1.查询条数
        Long count = departmentMapper.queryCount(query);
        //2.如果一条都没有，直接返回默认值
        if (count == 0){
            return  new PageList<>();
        }
        System.out.println(111111);
        //3.查询数据
        List<Department> departments = departmentMapper.queryData(query);
        //4.返回分页数据
        return new PageList<>(count,departments);
    }
}